package cn.xhc.head.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xhc.common.controller.BaseController;
import cn.xhc.common.util.ContantInfoUtil;
import cn.xhc.common.util.PageInfoUtil;
import cn.xhc.question.pojo.AColumn;
import cn.xhc.question.pojo.AItem;
import cn.xhc.question.pojo.CommonEnum;
import cn.xhc.question.service.IQuestionService;
/**
 * 免登录的首页
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/head")
public class IndexHeadController extends BaseController{
	@Resource
	private IQuestionService questionService;
	/**
	 * 首页
	 
	 * @return
	 */
	@RequestMapping("main")
	public String main(){
		ContantInfoUtil.LOGGER.info("--main--");
		return "/head/main";
	}
	/**
	 * 获取所有栏目信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/header")
	public String header(HttpServletRequest request){
		ContantInfoUtil.LOGGER.info("--header--");
		Map<String, Object> condMap = new HashMap<String,Object>();
		/*获取所有已启用的栏目信息*/
		condMap.put("status",CommonEnum.STATUS_ENABLED.getFlag());
		List<AColumn> columnList = this.questionService.findCondListColumnService(condMap);
		request.setAttribute("columnList", columnList);
		return "head/header";
	}
	/**
	 * 栏目下的测试题
	 * @return
	 */
	@RequestMapping("/{url}")
	public String itemList(HttpServletRequest request,@PathVariable String url){
		ContantInfoUtil.LOGGER.info("---url----{}",url);
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*根据url获取column*/
		condMap.clear();
		condMap.put("url",url+"");
		AColumn column = this.questionService.findOneColumnService(condMap);
		request.setAttribute("column", column);
		/*封装分页信息*/
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/*查询总记录数*/
		condMap.clear();
		condMap.put("columnsId",column.getId());
		int totalRecord = this.questionService.findCountItemList(condMap);
		pageInfoUtil.setTotalRecord(totalRecord);
		/*查询url对应的items信息(已启用)*/
		condMap.clear();
		condMap.put("columnsId",column.getId());
		condMap.put("status",CommonEnum.STATUS_ENABLED.getFlag());
		List<AItem> itemList = this.questionService.findCondListItemService(pageInfoUtil, condMap);
		request.setAttribute("itemList", itemList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "head/itemList";
	}
	/**
	 * 测试题的详情
	 * @return
	 */
	@RequestMapping("/item/{tId}")
	public String itemInfo(HttpServletRequest request,@PathVariable int tId){
		ContantInfoUtil.LOGGER.info("---itemInfo----");
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/*根据id查询测试项*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id",tId);
		AItem item = this.questionService.findOneItemService(condMap);
		request.setAttribute("item", item);
		/*根据columnId获取栏目下的所有item*/
		condMap.clear();
		condMap.put("columnsId", item.getColumnsId());
		/*根据columnId获取column*/
		AColumn column = item.getColumn();
		List<AItem> itemList = this.questionService.findCondListItemService(pageInfoUtil, condMap);
		request.setAttribute("column", column);
		request.setAttribute("itemList", itemList);
		/*查询上一篇*/
		condMap.clear();
		condMap.put("pubTimeDa",item.getPubTime());
		condMap.put("orderBy","pubTimeASC");
		List<AItem> itemListPre = this.questionService.findCondListItemService(pageInfoUtil, condMap);
		request.setAttribute("itemListPre", itemListPre);
		
		/*查询下一篇*/
		condMap.clear();
		condMap.put("pubTimeXiao",item.getPubTime());
		List<AItem> itemListNext = this.questionService.findCondListItemService(pageInfoUtil, condMap);
		request.setAttribute("itemListNext", itemListNext);
		return "head/itemInfo";
	}
	/**
	 * 查询测试项的所有问题
	 * @param request
	 * @param tId 测试项id
	 * @return
	 */
	@RequestMapping("/item/{tId}/start")
	public String questionList(HttpServletRequest request,@PathVariable int tId){
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*根据id查询测试项*/
		condMap.put("id",tId);
		AItem item = this.questionService.findOneItemService(condMap);
		/*根据columnI的获取column*/
		AColumn column = item.getColumn();
		/*获取column下的itemList*/
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		List<AItem> itemList = this.questionService.findCondListItemService(pageInfoUtil, condMap);
		/*将测试项放入作用域*/
		request.setAttribute("item", item);
		request.setAttribute("column", column);
		request.setAttribute("itemList", itemList);
		return "head/questionList";
	}
}
